//
//  BGMyEntrustController.m
//  bigMall
//
//  Created by ios on 16/9/1.
//  Copyright © 2016年 thinklev. All rights reserved.
//

#import "BGMyEntrustController.h"
#import "BGDateResult.h"
#import "BGOwnMessageListResultModel.h"
#import "BGZNXCell.h"
#import "BGEntrustDetailController.h"

@interface BGMyEntrustController () {
    
    NSInteger index;
    NSMutableArray *_dataArr;
    UIImageView *imageView;
    UILabel *lable;
    
}

@end

@implementation BGMyEntrustController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
//     self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self createUi];
    [self initDat];
}
- (void)initDat {
    
    _dataArr = [NSMutableArray array];
    
}
- (void)createUi {
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, BGWidth, BGHight - 106) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    //刷新的加载
    self.tableView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(index)];
    
    [self.tableView.header beginRefreshing];
    //判断数据源有无
    imageView = [[UIImageView alloc] initWithFrame:CGRectMake((BGWidth - 70 ) / 2 + 10, (self.tableView.height - 109 - 160) / 3, 70, 90)];
    imageView.image = [UIImage imageNamed:@"抱歉~暂无数据"];
    
    lable = [[UILabel alloc] initWithFrame:CGRectMake((BGWidth - 120 ) / 2 + 10, imageView.bottom + 21, 120, 16)];
    lable.text = @"抱歉~暂无数据";
    lable.font = [UIFont systemFontOfSize:16];
    [self.tableView addSubview:lable];
    
    [self.tableView addSubview:imageView];
    lable.hidden = YES;
    imageView.hidden = YES;
    
}
- (void)addIndex {
    
    index++;
    [self loadInter];
    
}
- (void)index {
    
    index = 1;
    [_dataArr removeAllObjects];
    [self loadInter];
    
}
- (void)loadInter {
    
    NSLog(@"type:%@",self.type);
    
    if ([self.controllerType isEqualToString:@"profile"]) {
    
    [BGDateResult owmSadListWithThisPage:[NSString stringWithFormat:@"%ld",(long)index]  pageNum:@"10" type:self.type success:^(NSObject *result) {
        
        BGOwnSadListResultModel *model = [[BGOwnSadListResultModel alloc] init];
        model = (BGOwnSadListResultModel *)result;
        //        NSLog(@"aaaa%@",model.status);
        //        NSLog(@"bbbb%@",model.list);
        NSArray *listArr = [NSArray arrayWithArray:model.list];
        
        for (BGOwnMessageListValueModel *a in listArr) {
            [_dataArr addObject:a];
        }
        
//        _dataArr = [NSMutableArray arrayWithArray:listArr];
        if (_dataArr.count == 0 && index == 1) {
            
            [self.tableView.header endRefreshing];
            [self.tableView.footer endRefreshingWithNoMoreData];
            
            //判断数据源有无
            lable.hidden = NO;
            imageView.hidden = NO;
            
        }
        if ([model.allPage intValue] > index) {
            self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(addIndex)];
        }
        if (listArr.count == 0) {
            [self.tableView.footer endRefreshingWithNoMoreData];
            return;
        }
      
        lable.hidden = YES;
        imageView.hidden = YES;
        [self.tableView.footer endRefreshing];
        [self.tableView.header endRefreshing];
        [self.tableView reloadData];

        
    } failure:^(NSError *error) {
        
    }];
        
    }else {
        
        [BGDateResult SadListWithThisPage:[NSString stringWithFormat:@"%ld",(long)index] pageNum:@"10" type:self.type success:^(NSObject *result) {
            
            BGOwnSadListResultModel *model = [[BGOwnSadListResultModel alloc] init];
            model = (BGOwnSadListResultModel *)result;
            //        NSLog(@"aaaa%@",model.status);
            //        NSLog(@"bbbb%@",model.list);
            NSArray *listArr = [NSArray arrayWithArray:model.list];
            for (BGOwnMessageListValueModel *a in listArr) {
                [_dataArr addObject:a];
            }

            if (_dataArr.count == 0 && index == 1) {
                
                [self.tableView.header endRefreshing];
                [self.tableView.footer endRefreshingWithNoMoreData];
                
                //判断数据源有无
                lable.hidden = NO;
                imageView.hidden = NO;
                
            }
            if ([model.allPage intValue] > index) {
                self.tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(addIndex)];
            }
            if (listArr.count == 0) {
                [self.tableView.footer endRefreshingWithNoMoreData];
                return;
            }
            lable.hidden = YES;
            imageView.hidden = YES;
            [self.tableView.footer endRefreshing];
            [self.tableView.header endRefreshing];
            [self.tableView reloadData];
            
        } failure:^(NSError *error) {
            
        }];
        
        
    }
  
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return _dataArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 90;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView registerClass:[BGZNXCell class] forCellReuseIdentifier:@"celId"];
    BGZNXCell *cel = [tableView dequeueReusableCellWithIdentifier:@"celId" forIndexPath:indexPath];
    BGOWnSadListValueModel *model = [[BGOWnSadListValueModel alloc] init];
    if (_dataArr.count != 0) {
        
        model = _dataArr[indexPath.row];
        cel.timeStr = model.add_time;
        cel.wuzhongStr = model.name;
        cel.remarkStr = model.cont;
    }
    
    return cel;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    BGEntrustDetailController *a = [[BGEntrustDetailController alloc] init];
    BGOWnSadListValueModel *model = [[BGOWnSadListValueModel alloc] init];
    if (_dataArr.count != 0) {
        
        model = _dataArr[indexPath.row];
        a.add_time = model.add_time;
        a.title1 = model.name;
        a.cont = model.cont;
        a.fromname = model.phone;
        a.imagUrl = model.profile_pic;
        
    }

    self.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:a animated:YES];
}
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return YES;
    
}
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return UITableViewCellEditingStyleDelete;
    
}
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        //删除对应数据的cell
        BGOWnSadListValueModel *model = [[BGOWnSadListValueModel alloc] init];
        if (_dataArr.count != 0) {
            
            model = _dataArr[indexPath.row];
            
        }
        
        NSLog(@"%@",model.idStr);
        
        [self delate:model.idStr];
        [_dataArr removeObjectAtIndex:indexPath.row];  //删除数组里的数据
        
        [tableView deleteRowsAtIndexPaths:[NSMutableArray arrayWithObject:indexPath]withRowAnimation:UITableViewRowAnimationAutomatic];
        
    }
    
}
- (void)delate:(NSString *)idStr {
    
    [BGDateResult deleteOwmSadWithIdstr:idStr success:^(NSObject *result) {
        
        BGsendMessageModel *model = (BGsendMessageModel *)result;
        if ([model.status isEqualToString:@"1"]) {
            [self showMessage:model.msg];
        } else {
            [self showMessage:model.msg];
        }
        
    } failure:^(NSError *error) {
        
    }];
    
    
}
- (void)showMessage:(NSString *)message {
    
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    
    [alertController addAction:okAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
    
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return @"删除";
    
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
- (void)viewWillAppear:(BOOL)animated {
    self.tabBarController.tabBar.hidden = YES;
    //    [UIApplication sharedApplication].statusBarHidden = YES;
}
@end
